package com.sun.controller.admin;

import com.sun.dto.ArtworkDTO;
import com.sun.dto.ArtworkPageQueryDTO;
import com.sun.entity.Artwork;
import com.sun.result.PageResult;
import com.sun.result.Result;
import com.sun.service.ArtworkService;
import com.sun.vo.ArtworkVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.List;
import java.util.Set;

/**
 * 艺术品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "艺术品相关接口")
public class ArtworkController {


    @Autowired
    private ArtworkService artworkService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 清除缓存数据
     * @param pattern
     */
    public void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    /**
     * 新增艺术品
     * @param artworkDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增艺术品")
    public Result save(@RequestBody ArtworkDTO artworkDTO) {
        log.info("新增艺术品:{}",artworkDTO);
        artworkService.saveWithDes(artworkDTO);

        //清除缓存数据
        String key = "artwork_" + artworkDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 艺术品分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("艺术品分页查询")
    public Result<PageResult> page(ArtworkPageQueryDTO artworkPageQueryDTO) {
        log.info("艺术品分页查询:{}",artworkPageQueryDTO);
        PageResult pageResult = artworkService.pageQuery(artworkPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 艺术品批量删除
     * @return
     */
    @DeleteMapping
    @ApiOperation("艺术品批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("艺术品批量删除:{}",ids);
        artworkService.deleteBatch(ids);

        //清除缓存数据
        cleanCache("artwork_*");

        return Result.success();

    }

    /**
     * 根据id查询艺术品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询艺术品")
    public Result<ArtworkVO> getById(@PathVariable Long id){
        log.info("根据id查询艺术品:{}", id);
        ArtworkVO artworkVo = artworkService.getByIdWithDes(id);
        return Result.success(artworkVo);
    }


    /**
     * 修改艺术品
     * @return
     */
    @PutMapping
    @ApiOperation("修改艺术品")
    public Result update(@RequestBody ArtworkDTO artworkDTO) {
        log.info("修改艺术品:{}",artworkDTO);

        artworkService.updateWithDes(artworkDTO);

        //清除缓存数据
        cleanCache("artwork_*");

        return Result.success();
    }

    /**
     * 艺术品起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("艺术品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        artworkService.startOrStop(status,id);

        cleanCache("artwork_*");
        return Result.success();
    }

    /**
     * 根据分类id查询艺术品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询艺术品")
    public Result<List<Artwork>> list(Long categoryId){
        List<Artwork> list = artworkService.list(categoryId);
        return Result.success(list);
    }


























}
